var ddapi = require('../../utils/md5.js');
var app = getApp();
Page({
  data: {
    config: {
      tipsshow: 'none',
      tipsshow_bool: false,
    },
    lengthconfig: {
      tipsshow: 'none',
      tipsshow_bool: false,
    },
    countconfig: {
      count: 0,
    },
    voteTitle: '',
    keyword: '',
    hiddenmodalput: true,
    map_length: 1000,
    map_scale: 16,
    map_top:36,
    swipertipsshow:'hide',
    hangye_id: 0,
    quan: [],
    quan_name: '生活圈',
    quan_id: '',
    quan_len: '',
    quan_center: '',
    latitude: 0,
    longitude: 0,
    mapHidden: false,
    listHidden: true,
    flag: true,
    ser_height: 36,
    topIndex: 0,
    width: 0,
    height: 0,
    real_height: 0,
    markers: [],
    shopList: [],
    circles: [],
    hangye:[],
    controls: [],
    titles: [],
    titles1: [
      1,
      2,
    ],
    lengthtitle: [
      '1km',
      '2km',
      '4km', 
      '8km'
    ],

    indicatorDots: false, //是否显示面板指示点
    autoplay: true, //是否自动切换
    interval: 3000, //自动切换时间间隔,3s
    duration: 1000, //  滑动动画时长1s

    indicatorDots1: true, 
    autoplay1: false, 
  },
  onLoad: function (options) {
    var that = this
    wx.setStorageSync('area_len', 1000);
    //小程序跳转获取用户信息
    if (options.openid != undefined && options.user_id != undefined) {
      var sign = ddapi.md5(options.openid + 'User.GetUserInfo' + options.user_id);
      wx.request({
        url: app.globalData.apiUrl + '?service=User.GetUserInfo',
        header: {
          'Content-Type': 'json'
        },
        data: {
          sign: sign,
          user_id: options.user_id,
          open_id: options.openid,
        },
        success: (res) => {
          var result = res.data.data;
          if (result.code == 0) {
            wx.setStorageSync('openid', options.openid);
            wx.setStorageSync('user_id', options.user_id);
            wx.setStorageSync('userPhone', result.info[0].tel);
          }
          console.log(wx.getStorageSync('userPhone'))
        }
      })
    }


    //获取系统信息  
    wx.getSystemInfo({
      //获取系统信息成功，将系统窗口的宽高赋给页面的宽高  
      success: function (res) {
        console.log(res.windowHeight)
        that.setData({
          width: res.windowWidth,
          height: res.windowHeight,
          real_height: res.windowHeight,
          controls : [
            {
              id: 'sys_1',
              iconPath: '/images/nav/map_5.png',
              position: {
                left: 10,
                top: 40 - 30,
                width: 40,
                height: 40
              },
              clickable: true
            }, 
            {
              id: 'sys_2',
              iconPath: '/images/nav/map_shangquan.png',
              position: {
                left: res.windowWidth - 40 - 10,
                top: 40 - 30,
                width: 40,
                height: 40
              },
              clickable: true
            },
             {
              id: 'sys_3',
              iconPath: '/images/nav/map_fenlei2.png',
              position: {
                left: res.windowWidth - 40 - 10,
                top: (res.windowWidth > 320) ? res.windowHeight - 30 - 260 : res.windowHeight - 30 - 220,
                width: 40,
                height: 40
              },
              clickable: true
            }, 
            {
              id: 'sys_4',
              iconPath: '/images/nav/map_pengyouquan.png',
              position: {
                left: res.windowWidth - 40 - 10,
                top: (res.windowWidth > 320) ? res.windowHeight - 30 - 130 : res.windowHeight - 30 - 120,
                width: 40,
                height: 40
              },
              clickable: true
            }, {
              id: 'sys_5',
              iconPath: '/images/nav/map_fanhui.png',
              position: {
                left: 10,
                top: (res.windowWidth > 320) ? res.windowHeight - 30 - 130 : res.windowHeight - 30 - 120,
                width: 40,
                height: 40
              },
              clickable: true
            }
          ]
        });
      }
    })
    that.notice();
  },
  onShow: function (e) {
    var that = this
    that.getAllArea();
  },
  onReady: function (e) {
    var that = this;
    wx.getLocation({
      type: 'gcj02', // 默认为 wgs84 返回 gps 坐标，gcj02 返回可用于 wx.openLocation 的坐标
      success: function (res) {
        console.log(res)
        that.setData({
          latitude: res.latitude,
          longitude: res.longitude,
          markers: [{
            id: 0,
            latitude: res.latitude,
            longitude: res.longitude
          }],
          circles: [{
            latitude: res.latitude,
            longitude: res.longitude,
            // color: '#FF69B4AA',
            // fillColor: '#FFB6C1AA',
            color: '#7cb5ecAA',
            fillColor: '#7cb5ec88',
            radius: that.data.length,
            strokeWidth: 1
          }],
        });
        that.getAddress();
        that.refreshHangye();
        that.refreshAreaShop();
        wx.setStorage({ key: 'latitude', data: res.latitude })
        wx.setStorage({ key: 'longitude', data: res.longitude })
      },
      fail: function () {
        wx.showToast({
          title: '定位错误',
        })
      }
    });
  },
  getAddress: function () {
    var that = this
    var lat = that.data.latitude + '';
    var lng = that.data.longitude + '';
    var service = 'Shop.GetAddress';
    var sign = ddapi.md5(lat + lng + service);

    wx.request({
      url: app.globalData.apiUrl + '?service=Shop.GetAddress',
      header: {
        'Content-Type': 'json'
      },
      data: {
        sign: sign,
        lat: lat,
        lng: lng
      },

      success: function (res) {
        wx.hideToast()
        console.log(res)
        var info = res.data.data.info
        wx.setStorage({ key: 'district', data: info.district })
        wx.setStorage({ key: 'city', data: info.city })
      }
    })
  },
  markertap: function (e) {
    console.log(e.markerId)
    var markerId = e.markerId;
    if (markerId != 0) {
      wx.navigateTo({
        url: "/pages/shop-detail/index?id="+markerId
      });
    }
  },
  callouttap: function (e) {
    console.log(e.markerId)
    var markerId = e.markerId;
    if (markerId != 0) {
      wx.navigateTo({
        url: "/pages/shop-detail/index?id=" + markerId
      });
    }
  },
  controltap: function (e) {
    var that = this
    var height = that.data.real_height;
    console.log(e.controlId);
    if (e.controlId == 'sys_1') {
      that.getShop();
    } else if (e.controlId == 'sys_3') {
      
      if (that.data.config.tipsshow_bool == false) {
        that.setData({
          config: {
            tipsshow_bool: true,
            tipsshow: 'hide',

          },
          height: height * ((height > 610) ? 0.71 : 0.64),
        })
        that.setData({
          lengthconfig: {
            tipsshow_bool: false,
            tipsshow: 'none'
          },
        })
        var length = that.data.map_length
        var mapScale = that.data.map_scale;
        that.setData({
          swipertipsshow: 'hide',
          flag: true,
          map_top: 36,
          ser_height: 36,
          map_length: length,
          map_scale: mapScale,
        })
      } else {
        that.setData({
          config: {
            tipsshow_bool: false,
            tipsshow: 'none'
          },
          height: height,
        })
      }
    } else if (e.controlId == 'sys_4') {
      if (that.data.lengthconfig.tipsshow_bool == false) {
        that.setData({
          lengthconfig: {
            tipsshow_bool: true,
            tipsshow: 'hide'
          },
          height: height * ((height > 610) ? 0.90 : 0.85),
        })
        var length = that.data.map_length
        var mapScale = that.data.map_scale;
        that.setData({
          swipertipsshow: 'hide',
          flag: true,
          map_top: 36,
          ser_height: 36,
          map_length: length,
          map_scale: mapScale,
        })
      } else {
        that.setData({
          lengthconfig: {
            tipsshow_bool: false,
            tipsshow: 'none'
          },
          height: height,
        })
      }
    } else if (e.controlId == 'sys_2'){
      wx.navigateTo({
        url: "/pages/lookshop/lookshop"
      });
    } else if (e.controlId == 'sys_5') {
      console.log(123456);
      var lat = wx.getStorageSync('latitude')+'';
      var lng = wx.getStorageSync('longitude')+'';
      console.log(lat);
      console.log(lng);
      that.setData({
        config: {
          tipsshow_bool: false,
          tipsshow: 'none'
        },
        latitude: lat,
        longitude: lng,
        // lengthconfig: {
        //   tipsshow_bool: false,
        //   tipsshow: 'none'
        // },
        // height: height,
        countconfig: {
          count: 0,
        },

        hangye_id: 0,
        map_length: 1000,
        map_scale: 16,
        quan_name: '生活圈',
        quan_id: '',
        quan_len: '',
        quan_center: '',
      }),

      wx.showToast({
        title: '重置中',
        icon: 'loading'
      })
      wx.setStorageSync('area_len', 1000)
      that.refreshAreaShop();
    }
  },

  areaCover: function (event) {
    var that = this
    var index = event.currentTarget.dataset['index'];
    console.log(index)

    wx.showToast({
      title: '加载中',
      icon: 'loading'
    })

    that.setData({
      hangye_id: index,
    })

    var center_jingwei = that.data.latitude + ',' + that.data.longitude;
    var hangye_id = index;
    var length = that.data.map_length;
    var openid = wx.getStorageSync('openid');
    var service = 'Shop.NewAreaShop';
    var user_id = wx.getStorageSync('user_id');
    var sign = ddapi.md5(center_jingwei + hangye_id + length + openid + service + user_id);
    console.log(center_jingwei + hangye_id + length + openid + service + user_id)
    console.log(sign)
    wx.request({
      url: app.globalData.apiUrl + '?service=Shop.NewAreaShop',
      header: {
        'Content-Type': 'json'
      },
      data: {
        sign: sign,
        center_jingwei: center_jingwei,
        hangye_id: hangye_id,
        length: length,
        openid: openid,
        user_id: user_id
      },
      success: function (res) {
        console.log(654123)
        console.log(res)
        var makers = [];
        var lookList = [];
        for (var i = 0; i < res.data.data.info.length; i++) {
          var dic = res.data.data.info[i];
          var latlng = dic.shop_jingwei.split(',');
        
          var micon = '/images/hangye_ico' + dic.shop_hangye.hangye_id + '.png';

          if (dic.shop_name.length == 5) {
            var xl = 0;
          } else if (dic.shop_name.length == 4) {
            var xl = 5;
          } else if (dic.shop_name.length == 3) {
            var xl = 10;
          } else if (dic.shop_name.length == 2) {
            var xl = 15;
          } else {
            var xl = 20;
          }

          
          var mid = dic.shop_id;
          console.log(mid);
          makers.push({
            iconPath: micon,
            id: mid,
            latitude: latlng[0],
            longitude: latlng[1],
            // width: 54 / 1.5,
            // height: 74 / 1.5,
            width: 30,
            height: 30,
            // callout: {
            //   content: dic.shop_name,
            //   fontSize: 13,
            //   color: '#e62a39',
            //   bgColor: '#ffffff',
            //   padding: 4,
            //   borderRadius: 6,
            //   borderWidth: 1,
            //   borderColor: "#e62a39",
            //   display: 'ALWAYS'
            // },
            label: {
              content: dic.shop_name,
              fontSize: 13,
              color: '#e62a39',
              anchorX: latlng[0] - (dic.shop_name.length * 10 + 23 + xl),
              anchorY: 5,
              bgColor: '#ffffff',
              textAlign: 'center',
              padding: 4,
              borderRadius: 6,
              borderWidth: 1,
              borderColor: "#e62a39",
              display: 'ALWAYS',
            },
          })
          var pic = makeShopPic(dic.yuanjiao_pic ? dic.yuanjiao_pic : dic.shop_pic_small, i);
          if (i < 19) {
            var name = dic.shop_name;
            if (name.length > 7) {
              name = name.substr(0, 7);
            }
            dic.shop_name = name;
            lookList.push(dic);
          }
      }

        lookList.push({
          shop_id: -1,
          shop_pic_small: "https://hongbao.zntech-china.com/attachment/images/wxcx/menu_more.png",
          shop_name: '更多'
        })

        that.setData({
          markers: makers,
          shopList: lookList
        })
        wx.hideToast() 
        console.log(makers)
      },
      fail: function () {
        wx.showToast({
          title: '网络错误',
        })
      }
    })
    var makeShopPic = function (pic_url, i) {

      wx.downloadFile({
        url: pic_url,
        success: function (pathInfo) {
          var cachePath = pathInfo.tempFilePath.replace("http:/", '').replace("https:/", '')
          var mak = "markers[" + i + "].iconPath";
          that.setData({
            [mak]: cachePath
          })
        }
      })
    }
  },

  lengthCover: function (event) {
    var that = this
    var index = event.currentTarget.dataset['index'];
    console.log(index)
    that.setData({
      countconfig: {
        count: index,
      },
    })
    
    var length = 1000;
    var mapScale = 14;

    if (index == 0) {
      length = 1000;
      mapScale = 16;
    } else if (index == 1) {
      length = 2000;
      mapScale = 15;
    } else if (index == 2) {
      length = 4000;
      mapScale = 14;
    } else {
      length = 8000;
      mapScale = 13;
    }
    wx.setStorageSync('area_len', length)
    that.setData({
      map_length: length,
      map_scale: mapScale,       
    });
    wx.showToast({
      title: '加载中',
      icon: 'loading'
    })
    that.refreshAreaShop();
  },
  getAllArea: function () {
    var that = this
    var openid = wx.getStorageSync('openid');
    var service = 'User.NewArea';
    var user_id = wx.getStorageSync('user_id');
    var sign = ddapi.md5(openid + service + user_id);
    wx.request({
      url: app.globalData.apiUrl + '?service=User.NewArea',
      header: {
        'Content-Type': 'json'
      },
      data: {
        sign: sign,
        open_id: openid,
        user_id: user_id
      },

      success: function (res5) {
        console.log(res5.data.data.info)
        var quan = []
        if (res5.data.data.info) {
          var arr = res5.data.data.info;
          for (var i = 0; i < arr.length; i++) {
            var dic = arr[i];
            if (dic.area_name != null && dic.area_name != '') {
              quan.push(dic)
            }
          }
        }

        that.setData({
          quan: quan
        });
      },
      fail: function () {
        wx.showToast({
          title: '网络错误',
        })
      }
    })
  },
  refreshAreaShop: function () {
    var that = this
    var center_jingwei = that.data.latitude + ',' + that.data.longitude;
    var hangye_id = that.data.hangye_id;
    var length = that.data.map_length;
    var mapScale = that.data.map_scale;

    var openid = wx.getStorageSync('openid');
    var service = 'Shop.NewAreaShop';
    var user_id = wx.getStorageSync('user_id');
    var sign = ddapi.md5(center_jingwei + hangye_id + length + openid + service + user_id);
    console.log(center_jingwei + hangye_id + length + openid + service + user_id)
    console.log(sign)

    that.setData({
      circles: [{
        latitude: that.data.latitude,
        longitude: that.data.longitude,
        // color: '#FF69B4AA',
        // fillColor: '#FFB6C1AA',
        color: '#7cb5ecAA',
        fillColor: '#7cb5ec88',
        radius: length,
        strokeWidth: 1
      }],
    })

    wx.request({
      url: app.globalData.apiUrl + '?service=Shop.NewAreaShop',
      header: {
        'Content-Type': 'json'
      },
      data: {
        sign: sign,
        center_jingwei: center_jingwei,
        hangye_id: hangye_id,
        length: length,
        openid: openid,
        user_id: user_id
      },
      success: function (res) {
        wx.hideToast()
        console.log(res)
        console.log(654123)
        var dic2 = res.data.data.info[0]
        wx.downloadFile({
          url: dic2.shop_pic_small,
          success:function(res2){
            console.log(res2)
            console.log(111111)
          }
        })
        var makers = [];
        var lookList = [];
        for (var i = 0; i < res.data.data.info.length; i++) {
          var dic = res.data.data.info[i];
          var latlng = dic.shop_jingwei.split(',');
          var micon = '/images/hangye_ico' + dic.shop_hangye.hangye_id + '.png';
          
          var mid = dic.shop_id;
          if (dic.shop_name.length == 5) {
            var xl = 0;
          } else if (dic.shop_name.length == 4) {
            var xl = 5;
          } else if (dic.shop_name.length == 3) {
            var xl = 10;
          } else if (dic.shop_name.length == 2) {
            var xl = 15;
          } else {
            var xl = 20;
          }
          makers.push({
            iconPath: micon,
            id: mid,
            latitude: latlng[0],
            longitude: latlng[1],
            // width: 54 / 1.5,
            // height: 74 / 1.5,
            width: 40,
            height: 40,
            // callout: {
            //   content: dic.shop_name,
            //   fontSize: 13,
            //   color: '#e62a39',
            //   bgColor: '#ffffff',
            //   padding: 6,
            //   borderRadius: 6,
            //   borderWidth: 1,
            //   borderColor: "#e62a39",
            //   display: 'ALWAYS'
            // },
            label: {
              content: dic.shop_name,
              fontSize: 13,
              color: '#e62a39',
              anchorX: latlng[0] - (dic.shop_name.length * 10 + 23 + xl),
              anchorY:5,
              bgColor: '#ffffff',
              textAlign: 'center',
              padding: 4,
              borderRadius: 6,
              borderWidth: 1,
              borderColor: "#e62a39",
              display: 'ALWAYS',
            },
          })
          var pic = makeShopPic(dic.yuanjiao_pic ? dic.yuanjiao_pic:dic.shop_pic_small, i);
          if (i < 19) {
            var name = dic.shop_name;
            if (name.length > 7) {
              name = name.substr(0, 7);
            }
            dic.shop_name = name;
            lookList.push(dic);
          }
        }

        lookList.push({
          shop_id: -1,
          shop_pic_small: "https://hongbao.zntech-china.com/attachment/images/wxcx/menu_more.png",
          shop_name: '更多'
        })

        that.setData({
          markers: makers,
          shopList: lookList
        })
        console.log(makers)
      },
      fail: function () {
        wx.showToast({
          title: '网络错误',
        })
      }
    })
    var makeShopPic= function (pic_url, i) {
      
      wx.downloadFile({
        url: pic_url,
        success: function (pathInfo) {
          var cachePath = pathInfo.tempFilePath.replace("http:/", '').replace("https:/", '')
          var mak = "markers[" + i + "].iconPath";
          that.setData({
            [mak]: cachePath
          })
        }
      })
    }
  },
  
  refreshHangye:function() {
    var that = this
    var openid = wx.getStorageSync('openid');
    var service = 'Shop.HangYe';
    var user_id = wx.getStorageSync('user_id');
    var sign = ddapi.md5(openid + service + user_id);
    console.log(sign);
    wx.request({
      url: app.globalData.apiUrl + '?service=Shop.HangYe',
      header: {
        'Content-Type': 'json'
      },
      data: {
        sign: sign,
        openid: openid,
        user_id: user_id
      },
      success: function (res) {
        console.log(res)
        that.setData({
          hangye: res.data.data.info
        })
      },
      fail: function () {
        wx.showToast({
          title: '网络错误',
        })
      }
    })
  },
  show: function () {
    var that = this
    var quan_num = that.data.quan.length
    var height = that.data.real_height;
    if (quan_num > 0) {
      if (that.data.flag == true) {
        that.setData({
          flag: false,
          map_top: 170,
          ser_height: 38,
          swipertipsshow: 'none',
        })
      } else {
        
        var length = that.data.map_length
        var mapScale = that.data.map_scale;
        that.setData({
          swipertipsshow: 'hide',
          flag: true,
          map_top: 36,
          ser_height: 36,
          map_length: length,
          map_scale: mapScale,
        })

      }
      that.setData({
        config: {
          tipsshow_bool: false,
          tipsshow: 'none'
        },
        lengthconfig: {
          tipsshow_bool: false,
          tipsshow: 'none'
        },
        height: height,
      })
    } else {
      wx.showToast({
        title: '暂无商圈'
      })
    }
  },
  showQuan: function (e) {
    console.log(e.currentTarget.dataset)
    var that = this
    var length = e.currentTarget.dataset.length
    var latlng = e.currentTarget.dataset.jingwei
    var latlng2 = latlng.split(',')

    var mapScale = 14;

    if (length == 1000) {
      mapScale = 16;
    } else if (length == 2000) {
      mapScale = 15;
    } else if (length == 4000) {
      mapScale = 14;
    } else if (length == 8000) {
      mapScale = 13;
    } else {
      length = 1000;
      mapScale = 16;
    }
    that.setData({
      swipertipsshow: 'hide',
      flag: true,
      map_top: 36,
      ser_height: 36,
      latitude: latlng2[0],
      longitude: latlng2[1],
      map_length: length,
      map_scale: mapScale,
      address: e.currentTarget.dataset.name,
      quan_name: e.currentTarget.dataset.name,
    })
    that.refreshAreaShop();
  },
  changeShow: function (e) {
    var that = this
    var show_id = e.currentTarget.dataset.id
    that.setData({
      show_id: show_id
    })
  },
  myRuzhu: function() {
    //检测是否已经获取用户信息
    var user_phone = wx.getStorageSync('userPhone');
    if (user_phone) {
      wx.navigateTo({
        url: "/pages/shop_admission/index"
      });
    } else {
      var url = '/pages/login/login';
      wx.navigateTo({
        url: url
      })
    }
  },
  wxSearchInput: function (e) {
    var that = this
    that.setData({
      keyword: e.detail.value
    })
  },
  wxSearchFn: function () {
    var that = this
    that.searchShop(that.data.keyword);
  },
  searchShop(keyword){
    wx.showToast({
      title: '搜索中',
      icon: 'loading'
    })
    var that = this
    var center_jingwei = that.data.latitude + ',' + that.data.longitude;
    var hangye_id = that.data.hangye_id;
    var length = that.data.map_length;
    var mapScale = that.data.map_scale;
    var kw = keyword;

    var openid = wx.getStorageSync('openid');
    var service = 'Shop.NewSearchShop';
    var user_id = wx.getStorageSync('user_id');
    var sign = ddapi.md5(center_jingwei + kw + length + openid + service + user_id);
    console.log(center_jingwei + kw + length + openid + service + user_id)
    console.log(sign)

    wx.request({
      url: app.globalData.apiUrl + '?service=Shop.NewSearchShop',
      header: {
        'Content-Type': 'json'
      },
      data: {
        sign: sign,
        center_jingwei: center_jingwei,
        length: length,
        openid: openid,
        user_id: user_id,
        keyword: kw
      },
      success: function (res) {
        wx.hideToast()
        console.log(res)
        var makers = [];
        var lookList = [];
        for (var i = 0; i < res.data.data.info.length; i++) {
          var dic = res.data.data.info[i];
          var latlng = dic.shop_jingwei.split(',');
        
          var micon = '/images/hangye_ico' + dic.shop_hangye.hangye_id + '.png';
          
          var mid = dic.shop_id;
          if (dic.shop_name.length == 5) {
            var xl = 0;
          } else if (dic.shop_name.length == 4) {
            var xl = 5;
          } else if (dic.shop_name.length == 3) {
            var xl = 10;
          } else if (dic.shop_name.length == 2) {
            var xl = 15;
          } else {
            var xl = 20;
          }

          makers.push({
            iconPath: micon,
            id: mid,
            latitude: latlng[0],
            longitude: latlng[1],
            // width: 54 / 1.5,
            // height: 74 / 1.5,
            width: 30,
            height: 30,
            // callout: {
            //   content: dic.shop_name,
            //   fontSize: 13,
            //   color: '#e62a39',
            //   bgColor: '#ffffff',
            //   padding: 4,
            //   borderRadius: 6,
            //   borderWidth: 1,
            //   borderColor: "#e62a39",
            //   display: 'ALWAYS',
            // },
            label: {
              content: dic.shop_name,
              fontSize: 13,
              color: '#e62a39',
              anchorX: latlng[0] - (dic.shop_name.length * 10 + 23 + xl), 
              anchorY:5,
              bgColor: '#ffffff',
              textAlign: 'center',
              padding: 4,
              borderRadius: 6,
              borderWidth: 1,
              borderColor: "#e62a39",
              display: 'ALWAYS',
            },
          })
          var pic = makeShopPic(dic.yuanjiao_pic ? dic.yuanjiao_pic : dic.shop_pic_small, i);
          if (i < 19) {
            var name = dic.shop_name;
            if (name.length > 7) {
              name = name.substr(0, 7);
            }
            dic.shop_name = name;
            lookList.push(dic);
          }
        }

        lookList.push({
          shop_id: -1,
          shop_pic_small: "https://hongbao.zntech-china.com/attachment/images/wxcx/menu_more.png",
          shop_name: '更多'
        })

        that.setData({
          markers: makers,
          shopList: lookList,
          keyword: ''
        })
        console.log(makers)
      },
      fail: function () {
        wx.showToast({
          title: '网络错误',
        })
      }
    })
    var makeShopPic = function (pic_url, i) {

      wx.downloadFile({
        url: pic_url,
        success: function (pathInfo) {
          var cachePath = pathInfo.tempFilePath.replace("http:/", '').replace("https:/", '')
          var mak = "markers[" + i + "].iconPath";
          that.setData({
            [mak]: cachePath
          })
        }
      })
    }
  },
  saveArea : function() {
    var that = this
    var real_height = that.data.real_height
    var hiddenmodalput = that.data.hiddenmodalput
    var tipsshow = that.data.lengthconfig.tipsshow
    var tipsshow_bool = that.data.lengthconfig.tipsshow_bool
    that.setData({
      hiddenmodalput: !hiddenmodalput,
      mapHidden: hiddenmodalput,
      height: (tipsshow_bool == true) ? real_height * ((real_height > 620) ? 0.85 : 0.80) : real_height,
      lengthconfig: {
        tipsshow: (!hiddenmodalput) ? tipsshow:'none',
        tipsshow_bool: (!hiddenmodalput) ? tipsshow : false,
      },
    })
  },
  cancel: function () {
    var that = this
    var real_height = that.data.real_height
    that.setData({
      hiddenmodalput: true,
      mapHidden: false,
      height: real_height * 0.80,
      lengthconfig: {
        tipsshow: 'hide',
        tipsshow_bool: true,
      },
    });
  },
  confirm: function (e) {
    var that = this
    var real_height = that.data.real_height
    var tipsshow_bool = that.data.lengthconfig.tipsshow_bool;
    if (that.data.voteTitle == '') {
      wx.showModal({
        title: "提示",
        content: "请输入商家名",
        showCancel: false
      })
    } else {
      this.setData({
        hiddenmodalput: true,
        mapHidden: false,
        height: real_height,
        lengthconfig: {
          tipsshow: 'none',
          tipsshow_bool: false,
        },
      })
      that.saveData();
    }
  },
  voteTitle: function (e) {
    var that = this
    that.data.voteTitle = e.detail.value;
  },
  saveData: function() {
    var that = this
    var service = 'User.NewSaveArea';
    var openid = wx.getStorageSync('openid');
    var user_id = wx.getStorageSync('user_id');
    var area_name = that.data.voteTitle;
    var center_jingwei = that.data.latitude + ',' + that.data.longitude;
    var length = that.data.map_length;
    var sign = ddapi.md5(area_name + center_jingwei + length + openid + service + user_id);
    wx.request({
      url: app.globalData.apiUrl + '?service=User.NewSaveArea',
      header: {
        'Content-Type': 'json'
      },
      data: {
        sign: sign,
        center_jingwei: center_jingwei,
        length: length,
        area_name: area_name,
        open_id: openid,
        user_id: user_id
      },

      success: function (res) {
        console.log(res)
        var code = res.data.data.code;
        if (code == 0) {
          that.getAllArea();
          wx.showModal({
            title: "提示",
            content: "保存成功",
            showCancel: false
          })
        } else {
          wx.showModal({
            title: "提示",
            content: "保存失败",
            showCancel: false
          })
        }
      },
      fail: function () {
        wx.showToast({
          title: '网络错误',
        })
      }
    })
  },
  getShop: function () {
    var that = this
    var service = 'User.GetMyShop';
    var openid = wx.getStorageSync('openid');
    var user_id = wx.getStorageSync('user_id');
    var shop_id = 0;
    var sign = ddapi.md5(openid + service + shop_id + user_id);
    var orderList = that.data.orderList;
    wx.request({
      url: app.globalData.apiUrl + '?service=User.GetMyShop',
      header: {
        'Content-Type': 'json'
      },
      data: {
        sign: sign,
        open_id: openid,
        user_id: user_id,
        shop_id: shop_id
      },
      success: function (res) {
        console.log(res)
        if (res.data.data.code == 0) {
          wx.navigateTo({
            url: "/pages/gongxiang/gongxiang"
          });
        } else {
          wx.showModal({
            title: "提示",
            content: "您还没有店铺，不能共享商品",
            showCancel: false
          })
        }
      },
      fail: function () {
        wx.showToast({
          title: '网络错误',
        })
      }
    })
  },
  notice: function() {
    var that = this
    var service = 'Shop.NoticeShop';
    var openid = wx.getStorageSync('openid');
    var user_id = wx.getStorageSync('user_id');
    var sign = ddapi.md5(openid + service + user_id);
    wx.request({
      url: app.globalData.apiUrl + '?service=Shop.NoticeShop',
      header: {
        'Content-Type': 'json'
      },
      data: {
        sign: sign,
        open_id: openid,
        user_id: user_id
      },

      success: function (res) {
        console.log(res)
        var code = res.data.data.code;
        if (code == 0) {
          that.setData({
            titles: res.data.data.info
          })
        }
      }
    })
  },
 
  topClick: function(e) {
    var that = this
    var index = e.currentTarget.dataset['index'];
    if (index == 0) {
      that.setData({
        mapHidden: false,
        listHidden: true,
        height: that.data.real_height,
        topIndex: 0,
      })
    } else {
      that.setData({
        swipertipsshow: 'hide',
        flag: true,
        map_top: 36,
        ser_height: 36,
        mapHidden: true,
        height:0,
        listHidden: false,
        topIndex: 1,
        config: {
          tipsshow_bool: false,
          tipsshow: 'none'
        },
        lengthconfig: {
          tipsshow_bool: false,
          tipsshow: 'none'
        },
      })
    }
  },
  pushShop: function (event) {
    var that = this
    var id = event.currentTarget.dataset['id'];
    console.log(id)
    if (id > 0) {
      wx.navigateTo({
        url: "/pages/shop-detail/index?id=" + id
      })
    } else {
      var url = '/pages/shangquan/shangquan';
      wx.switchTab({
        url: url,
      });
    }
  }
})


